package com.ruoyi.manage.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.DeviceNetworkCardMapper;
import com.ruoyi.manage.domain.DeviceNetworkCard;
import com.ruoyi.manage.service.IDeviceNetworkCardService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备网卡信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-23
 */
@Service
public class DeviceNetworkCardServiceImpl implements IDeviceNetworkCardService
{
    @Autowired
    private DeviceNetworkCardMapper deviceNetworkCardMapper;

    /**
     * 查询设备网卡信息
     * 
     * @param cardId 设备网卡信息主键
     * @return 设备网卡信息
     */
    @Override
    public DeviceNetworkCard selectDeviceNetworkCardByCardId(Long cardId)
    {
        return deviceNetworkCardMapper.selectDeviceNetworkCardByCardId(cardId);
    }

    /**
     * 根据设备编码查询设备网卡信息列表
     * 
     * @param deviceCode 设备编码
     * @return 设备网卡信息集合
     */
    @Override
    public List<DeviceNetworkCard> selectDeviceNetworkCardByDeviceCode(String deviceCode)
    {
        return deviceNetworkCardMapper.selectDeviceNetworkCardByDeviceCode(deviceCode);
    }

    /**
     * 查询设备网卡信息列表
     * 
     * @param deviceNetworkCard 设备网卡信息
     * @return 设备网卡信息集合
     */
    @Override
    public List<DeviceNetworkCard> selectDeviceNetworkCardList(DeviceNetworkCard deviceNetworkCard)
    {
        return deviceNetworkCardMapper.selectDeviceNetworkCardList(deviceNetworkCard);
    }

    /**
     * 新增设备网卡信息
     * 
     * @param deviceNetworkCard 设备网卡信息
     * @return 结果
     */
    @Override
    public int insertDeviceNetworkCard(DeviceNetworkCard deviceNetworkCard)
    {
        return deviceNetworkCardMapper.insertDeviceNetworkCard(deviceNetworkCard);
    }

    /**
     * 修改设备网卡信息
     * 
     * @param deviceNetworkCard 设备网卡信息
     * @return 结果
     */
    @Override
    public int updateDeviceNetworkCard(DeviceNetworkCard deviceNetworkCard)
    {
        return deviceNetworkCardMapper.updateDeviceNetworkCard(deviceNetworkCard);
    }

    /**
     * 批量删除设备网卡信息
     * 
     * @param cardIds 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteDeviceNetworkCardByCardIds(Long[] cardIds)
    {
        return deviceNetworkCardMapper.deleteDeviceNetworkCardByCardIds(cardIds);
    }

    /**
     * 删除设备网卡信息
     * 
     * @param cardId 设备网卡信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceNetworkCardByCardId(Long cardId)
    {
        return deviceNetworkCardMapper.deleteDeviceNetworkCardByCardId(cardId);
    }

    /**
     * 保存设备网卡信息列表
     * 
     * @param deviceCode 设备编码
     * @param networkCardList 网卡信息列表
     * @return 结果
     */
    @Override
    @Transactional
    public int saveDeviceNetworkCardList(String deviceCode, List<DeviceNetworkCard> networkCardList)
    {
        // 先删除该设备的所有网卡信息
        deviceNetworkCardMapper.deleteDeviceNetworkCardByDeviceCode(deviceCode);
        
        // 再批量插入新的网卡信息
        int count = 0;
        if (networkCardList != null && !networkCardList.isEmpty())
        {
            for (DeviceNetworkCard networkCard : networkCardList)
            {
                networkCard.setDeviceCode(deviceCode);
                count += deviceNetworkCardMapper.insertDeviceNetworkCard(networkCard);
            }
        }
        return count;
    }
}